//
//  MBDMainViewModel.swift
//  MasterBoy
//
//  Created by kyson on 2023/4/22.
//

import Foundation
import YYText
import SnapKit
import Alamofire
import SwiftyJSON
import Toast_Swift
import MJRefresh
import Foundation
import JLRoutes

typealias KPosterAvatarClickedBlock = () ->()

class MBDMainViewModel : NSObject {
    public static let pageSize = 10
    
    var cellModels : Array<MBDMainCellModel>?    
    
    func doRequest(pageNum : Int = 1, completion : @escaping (_ error : NSError?, _ result : Array<MBDMainCellModel>?,_ totalPage : Int)->()) {
        
        MBDHttpClient.shared.doGetRequest("/group/listPage",parameters: ["pageNum":String(pageNum) ]).httpClientResponse { error, response in
            
            if nil != error {
                completion(error,nil,0)
            } else {
                
                if let data = response?.responseObject  {
                    
                    let totalPage = response?.rawDict?["totalPage"] as! Int
                    if data is Array<Any> {
                        let rows = data as! Array<Any>
                        var multiRows = Array<MBDMainCellModel>()
                        for row in rows {
                            let item = MBDGroupDetailModel(JSON(rawValue: row) ?? JSON(NSObject()))
                            let cellModel = MBDMainCellModel()
                            cellModel.groupDetail = item
                            multiRows.append(cellModel)
                        }
                        completion(nil,multiRows,totalPage)
                    } else {
                        let er = NSError(domain: "ServerErrorDomain", code: 40001,userInfo: [NSLocalizedDescriptionKey:"服务器貌似出了点问题"])
                        completion(er,nil,0)
                    }
        
                } else {
                    let er = NSError(domain: "ServerErrorDomain", code: 400002,userInfo: [NSLocalizedDescriptionKey:"服务器貌似出了点问题"])
                    completion(er,nil,0)
                }
            }
        }
        
    }
    
    
}
